
package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.ecception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.GifCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    SystemUserService service;

    @Override
    public CaptchaVo getCaptcha() {
        // png类型 静态图片验证码
        /* SpecCaptcha captcha = new SpecCaptcha(130, 48);*/
        GifCaptcha captcha = new GifCaptcha(130, 48);
        String code = captcha.text().toLowerCase();// 获取验证码的字符
        String img = captcha.toBase64(); //生成图片

        //把code验证码值 保存到redis并设置有效期
        String codeKey= RedisConstant.ADMIN_LOGIN_PREFIX+ UUID.randomUUID();
        redisTemplate.opsForValue().set(codeKey,code,1, TimeUnit.HOURS);

        CaptchaVo captchaVo=new CaptchaVo(img,codeKey);

        return captchaVo;
    }

    @Override
    public String login(LoginVo loginVo) {

        String username = loginVo.getUsername();
        String password = loginVo.getPassword();

        String captchaCode = loginVo.getCaptchaCode();
        String captchaKey = loginVo.getCaptchaKey();


        //1. 判断验证码是否为空

        if(captchaCode==null){
            throw  new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }

        //2.获取redis中的验证码,进行比较
        Object redisCode = redisTemplate.opsForValue().get(captchaKey);
        if(redisCode==null||!captchaCode.equals(redisCode)){
            throw  new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //3. 根据账号查询用户的信息(判断用户是否存在)
        SystemUser user = service.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, username));
        if(user==null){
            throw  new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //4. 查看账号是否被禁用
        if(user.getStatus()== BaseStatus.DISABLE){
            throw  new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }

        //5. 比较密码
        System.out.println(DigestUtils.md5Hex(password));
        String datebase_password = DigestUtils.md5Hex(password);
        System.out.println("数据库里的密码:"+datebase_password);
        System.out.println("用户的密码:"+user.getPassword());
        if(!user.getPassword().equals(datebase_password)){
            throw  new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }


        //6. 创建token
        String token = JwtUtil.createToken(user.getUsername(), user.getId());

        return token;
    }


}

